package com.heima.thread.cookandfoodie.waitAndNotify;

public class Foodie extends Thread{

    @Override
    public void run() {
        while(true)
        {
            synchronized (Desk.lock){
                if(Desk.count == 0){
                    break;
                }else{
                    // 先判断桌子上是否有面条
                    if(Desk.foodflag == 0){
                        // 如果没有, 就等待
                        System.out.println("没得吃");
                        try {
                            Desk.lock.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }else{
                        // 把吃的总数-1
                        Desk.count -= 1;
                        // 如果有, 就开吃
                        System.out.println("吃货正在吃面条, 还能吃"+Desk.count+"碗");
                        // 吃完之后, 唤醒厨师继续做
                        Desk.lock.notifyAll();
                        // 修改桌子的状态
                        Desk.foodflag = 0;
                    }
                }
            }
        }
    }
}
